<?php
namespace app\index\controller;

use Jssdk\JSSDK;
use think\View;

class Index extends CheckMember
{
    /**
     * 就行首页
     * @return \think\response\View
     */
    public function index()
    {
//        dump(json_decode('{"content":"好","user":{"name":"vv5","icon":"http:\/\/oxncaiuir.bkt.clouddn.com\/b300520171026104457474.jpg-100","id":"428"}}', true));
//        echo md5('jgsdfjkfgjs#$%&khs2423hkfhali_app_pay');
//        $des3 = new \Des3\DES3();
//        echo $des3->decrypt('tHgEHCTxLnGbVCLIhyNR2A==');exit;
        $param = input('param.');
        $this->assign('lat', isset($param['lat']) ? $param['lat'] : '');
        $this->assign('lng', isset($param['lng']) ? $param['lng'] : '');

        $data = $this->local_invoke_api('get_index_info', ['member_id' => session('member_id')]);
        $this->assign('data', $data['data']);

        if (is_weixin()) {
            $jssdk = new JSSDK(config('wechat.appid'), config('wechat.appsecret'));
            $signPackage = $jssdk->getSignPackage();
            $this->assign('signPackage', $signPackage);
        }


        return view();
    }

    /**
     * 根据分类和经纬度获取店铺列表
     * @return \think\response\View
     */
    public function get_shop_list_by_category_and_lat_lng() {
        $param = input('param.');
        $param['member_id'] = session('member_id');
        $data = $this->local_invoke_api('get_shop_list_by_category_and_lat_lng', $param);
        return json($data);
    }

    /**
     * ajax获取店铺信息
     * @return \think\response\View
     */
    public function get_shop_info() {
        $id = input('param.id');
        $data = local_invoke_api('get_shop_info', ['id' => $id]);
        $view = new View();
        $view->assign('data', $data['data']);
        $tpl = $view->fetch('shop_info_tpl');
        $data['tpl'] = $tpl;
        return json($data);
    }

    /**
     * 创建需求订单
     * @return \think\response\View
     */
    public function create_demand_order(){
        $param = input('param.');
        $param['member_id'] = session('member_id');
        $data = $this->local_invoke_api('create_demand_order', $param);
        return json($data);
    }

    /**
     * 根据经纬度获取店铺列表
     * @return \think\response\Json
     */
    public function shop_list() {
        $param = input('param.');
        $data = $this->local_invoke_api('get_shop_list_by_lat_lng', $param);
        $this->assign('lat', $param['lat']);
        $this->assign('lng', $param['lng']);
        $this->assign('data', $data['data']);
        return view();
    }

    /**
     * 店铺详情页面
     * @return \think\response\View
     */
    public function shop_info() {
        $param = input('param.');
        $data = $this->local_invoke_api('get_shop_info', $param);
        $this->assign('data', $data['data']);
        return view();
    }

    /**
     * 获取店铺评论列表
     * @return \think\response\Json
     */
    public function get_shop_comment() {
        $id = input('param.id');
        $data = local_invoke_api('get_shop_comment_list', ['shop_id' => $id]);
        $view = new View();
        $view->assign('data', $data['data']);
        $tpl = $view->fetch('shop_comment_tpl');
        $data['tpl'] = $tpl;
        return json($data);
    }

    public function test() {
        //echo 123;
        $url = 'localhost:8078/interface/';
        $action = 'upload_image';
        $sign = md5('jgsdfjkfgjs#$%&khs2423hkfh' . $action);
        $data = [
            'image_type' => 0
        ];
        $data = post_data($url.$action, 'sign=' . $sign . '&data=' .json_encode($data));
        dump($data);
    }

    public function qiniu_test() {
        if (request()->isPost()) {
            $data = \app\api\service\Upload::upload_image();
            return json($data);
        } else {
            return view();
        }
    }

    public function jcrop() {
        return view();
    }

    public function map_test() {
        return view();
    }
}
